package ext.mango.ab.po;

import java.util.List;

import mango.functions.FuncString;
import mango.orm.Dao;
import mango.orm.DaoFactory;
import mango.orm.DBSession;
import mango.orm.DBSessionFactory;
import mango.orm.entity.Entity;
import mango.service.ServiceManager;
import mango.web.Info;
import ext.mango.ab.eo.EOABHouseKeeper;
import ext.mango.ab.so.SOABHouseKeeper;
import ext.mango.base.POBase;

public class HouseKeeper extends POBase {

    
    public boolean readcode() {
        String type = this.getRequestParameter("type");
        if ("spendtype".equals(type)) {
//            String sql = "select code||'-'||codename spendtype from basecode where codetype='spendtype' order by code";
            String sql = "select concat(code,'-',codename) spendtype from basecode where codetype='spendtype' and (other=? or other='default') order by code";
            List<Entity> list = DaoFactory.c(sql).setParameter(this.getCurrentUser().getId()).listQuery();
            this.setResult(Entity.toJSON(list, type));
        }
        else if ("spendtypeitem".equals(type)) {
            String spendType = this.getRequestParameter("spendtype");
            spendType = spendType.substring(0,spendType.indexOf("-"));
            String sql = "select item from ABHouseKeeperTypeItems where spendType=? and operator=?";
            List<Entity> list = DaoFactory.c(sql).setParameter(spendType, this.getCurrentUser().getId()).listQuery();
            this.setResult(Entity.toJSON(list, type));
        }
        return true;
    }
    
    public boolean r(){
        String sql = "select id, FUNC_AA_GETCODENAME('spendtype',spendtype) spendtype, money,items,actiondate from ABHouseKeeper where operator=@userid ";
        String startDate = this.getRequestParameter("STARTDATE");
        String endDate = this.getRequestParameter("ENDDATE");
        if (!FuncString.isBlank(startDate)) {
//            sql += " and to_char(actiondate, 'yyyy-mm-dd')>=@STARTDATE";
            sql += " and actiondate>=@STARTDATE";
        }
        if (!FuncString.isBlank(endDate)) {
//            sql += " and to_char(actiondate, 'yyyy-mm-dd')<=@ENDDATE";
            sql += " and actiondate<=@ENDDATE";
        }
        Dao dao = DaoFactory.c(sql)
                .setParamByName("userid", this.getCurrentUser().getId())
                .setParamByName("STARTDATE", startDate)
                .setParamByName("ENDDATE", endDate)
                .setStart(this.getRequestInt("start"))
                .setLimit(this.getRequestInt("limit"));
        int totalCount = dao.getTotalCount();
        this.setResult(Entity.toJSON(dao.listQuery(), "housekeeper", totalCount));
        return true;
    }

    public boolean u(){
        EOABHouseKeeper eoHouseKeeper = new EOABHouseKeeper().setID(this.getRequestParameter("ID"))
                .setMoney(this.getRequestParameter("MONEY"))
                .setItems(this.getRequestParameter("ITEMS"))
                .setSpendType(this.getRequestParameter("SPENDTYPE"))
                .setActionDate(this.getRequestDate("ACTIONDATE"))
                .setOperator(this.getCurrentUser().getId());

        SOABHouseKeeper soHouseKeeper = ServiceManager.getService(SOABHouseKeeper.class);
        boolean ret = soHouseKeeper.saveHouseKeeper(eoHouseKeeper, this);
        if (ret) {
            this.addInfo(Info.SUCCESS, eoHouseKeeper.getID());
        }
        return ret;
    }
    
    public boolean d(){
        String id = this.getRequestParameter("ID");
        String sql = "delete from ABHouseKeeper where id=?";
        String sql2 = "delete from ABHouseKeeperDetail where pid=?";
        DBSession session = DBSessionFactory.getSession();
        session.execute(sql, id);
        session.execute(sql2, id);
        session.commit();
        return true;
    }
}
